<?php
/**
 * Skyline :: Patterns & component framework
 * Copyright 2010-2011 Ian Unruh (Xerof3ar)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Skyline\Structure\Map;

use InvalidArgumentException;
use LogicException;
use SplObjectStorage;
use Skyline\Comparator\ComparatorInterface;

/**
 * Map that is backed by an SplObjectStorage instance that only uses objects
 * for keys; allows for very efficient object maps
 * 
 * @todo Implement key sorting
 * @author Ian Unruh (Xerof3ar) <ianunruh@gmail.com>
 * @since Class avaliable since release 1.0
 */
class ObjectMap extends AbstractMap
{
	/**
	 * @var SplObjectStorage<object:mixed>
	 */
	protected $entries;
	
	/**
	 * Class constructor
	 * @return void
	 */
	public function __construct()
	{
		$this->entries = new SplObjectStorage();
	}
	
	/**
	 * Clears this container of all its elements
	 * @return void
	 */
	public function clear()
	{
		$this->entries = new SplObjectStorage();
	}
	
	/**
	 * Returns true if a given key exists in this map
	 * 
	 * @param mixed $key
	 * @return bool
	 */
	public function containsKey($key)
	{
		return $this->entries->offsetExists($key);
	}
	
	/**
	 * Returns true if a given value exists in this map
	 * 
	 * @param mixed $value
	 * @return bool
	 */
	public function containsValue($value)
	{
		foreach($this->entries as $key) {
			if($value === $this->entries->getInfo()) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Returns the value associated with a given key (if any)
	 * 
	 * @param mixed $key
	 * @return mixed|null Returns null if key not in map
	 */
	public function get($key)
	{
		if($this->entries->offsetExists($key)) {
			return $this->entries->offsetGet($key);
		}
	}
	
	/**
	 * Returns a set of the keys in this map
	 * @return SetInterface<mixed>
	 */
	public function getKeys()
	{
		$set = $this->createSet();
		foreach($this->entries as $key) {
			$set->add($key);
		}
		return $set;
	}
	
	/**
	 * Returns a set of the values in this map
	 * @return SetInterface<mixed>
	 */
	public function getValues()
	{
		$set = $this->createSet();
		foreach($this->entries as $key) {
			$set->add($this->entries->getInfo());
		}
		return $set;
	}
	
	/**
	 * Returns the first key associated with a given value (if any)
	 * 
	 * @param mixed $value
	 * @return mixed|null Returns null if no key is associated with the value
	 */
	public function keyOf($value)
	{
		foreach($this->entries as $key) {
			if($value === $this->entries->getInfo()) {
				return $key;
			}
		}
	}
	
	/**
	 * Returns all keys associated with a given value
	 * 
	 * @param mixed $value
	 * @return mixed[]
	 */
	public function keysOf($value)
	{
		$keys = array();
		foreach($this->entries as $key) {
			if($value === $this->entries->getInfo()) {
				$keys[] = $key;
			}
		}
		return $keys;
	}
	
	/**
	 * Removes a key from the map
	 * 
	 * @param mixed $key
	 * @return void
	 */
	public function remove($key)
	{
		$this->entries->offsetUnset($key);
		$this->changed = true;
	}
	
	/**
	 * Associates a given key with a given value in the map
	 * 
	 * @param mixed $key
	 * @param mixed $value
	 * @return void
	 */
	public function set($key, $value)
	{
		if(!is_object($key)) {
			throw new InvalidArgumentException('Only object keys are supported by ObjectMap');
		}
		$this->assertNotFull();
		$this->assertEntryIsValid($key, $value);
		$this->entries->offsetSet($key, $value);
	}
	
	/**
	 * Returns this map in the form of an associative array (if possible)
	 * 
	 * @throws LogicException If operation is unsupported
	 * @return mixed[mixed]
	 */
	public function toArray()
	{
		throw new LogicException('Operation not supported by ObjectMap');
	}
	
	/**
	 * Returns the number of entries in the map
	 * @return int
	 */
	public function count()
	{
		return $this->entries->count();
	}
	
	/**
	 * Returns an iterator for the entries in the map
	 * @return Iterator
	 */
	public function getIterator()
	{
		return $this->entries;
	}
	
	/**
	 * Sets the comparator used to sort this map
	 * 
	 * @param ComparatorInterface $comparator
	 * @return void
	 */
	public function setComparator(ComparatorInterface $comparator)
	{
		throw new LogicException('Operation not supported by ObjectMap');
	}
}